﻿using System;
using System.Web.UI;
using JQuery.WebKit.Base;
using JQuery.WebKit.WebCommon;
using JQuery.WebKit.WebCommon.Attributes;

#region resources
[assembly: WebResource("JQuery.WebKit.AeroWindow.js.jquery-AeroWindow.js", "text/javascript", PerformSubstitution = true)]
[assembly: WebResource("JQuery.WebKit.AeroWindow.js.jquery-AeroWindow.min.js", "text/javascript", PerformSubstitution = true)]
[assembly: WebResource("JQuery.WebKit.AeroWindow.css.AeroWindow.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.active.bl.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.active.bm.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.active.br.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.active.btn-close.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.active.btn-max.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.active.btn-min.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.active.btn-reg.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.active.lines.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.active.lm.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.active.mm.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.active.rm.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.active.tl.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.active.tm.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.active.tr.png", "image/png")]

[assembly: WebResource("JQuery.WebKit.AeroWindow.images.inactive.bl.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.inactive.bm.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.inactive.br.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.inactive.btn-close.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.inactive.btn-max.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.inactive.btn-min.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.inactive.btn-reg.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.inactive.lines.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.inactive.lm.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.inactive.mm.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.inactive.rm.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.inactive.tl.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.inactive.tm.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.inactive.tr.png", "image/png")]

[assembly: WebResource("JQuery.WebKit.AeroWindow.images.hover.btn-close.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.hover.btn-max.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.hover.btn-min.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.hover.btn-reg.png", "image/png")]

[assembly: WebResource("JQuery.WebKit.AeroWindow.images.empty.gif", "image/gif")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.left.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.right.png", "image/png")]
[assembly: WebResource("JQuery.WebKit.AeroWindow.images.spacer.png", "image/png")]

#endregion

namespace JQuery.WebKit
{
    public class AeroWindow : WebControlBase, INamingContainer
    {
        private bool? _autoOpen;
        [JavaScript("AutoOpen", Category = "options", DefaultValue = false)]
        public bool? AutoOpen
        {
            get { return _autoOpen; }
            set
            {
                _autoOpen = value;
                if (value.HasValue && value.Value)
                {
                    WindowStatus = "regular";
                    CloseAfterSubmit = false;
                }
            }
        }

        [JavaScript("UpdaterID", Category = "options", DefaultValue = null)]
        public JUpdater Updater
        {
            get;
            private set;
        }

        [JavaScript("WindowTitle", Category = "options", DefaultValue = "Window")]
        public string WindowTitle
        {
            get;
            set;
        }

        [JavaScript("WindowPositionTop", Category = "options", DefaultValue = "60")]
        public string WindowPositionTop
        {
            get;
            set;
        }

        [JavaScript("WindowPositionLeft", Category = "options", DefaultValue = "10")]
        public string WindowPositionLeft
        {
            get;
            set;
        }

        [JavaScript("WindowWidth", Category = "options", DefaultValue = 0)]
        public int? WindowWidth
        {
            get;
            set;
        }

        [JavaScript("WindowHeight", Category = "options", DefaultValue = 0)]
        public int? WindowHeight
        {
            get;
            set;
        }

        [JavaScript("WindowMinWidth", Category = "options", DefaultValue = 250)]
        public int? WindowMinWidth
        {
            get;
            set;
        }

        [JavaScript("WindowMinHeight", Category = "options", DefaultValue = 250)]
        public int? WindowMinHeight
        {
            get;
            set;
        }

        [JavaScript("WindowResizable", Category = "options", DefaultValue = true)]
        public bool? WindowResizable
        {
            get;
            set;
        }

        [JavaScript("WindowMaximize", Category = "options", DefaultValue = true)]
        public bool? WindowMaximize
        {
            get;
            set;
        }

        [JavaScript("WindowMinimize", Category = "options", DefaultValue = true)]
        public bool? WindowMinimize
        {
            get;
            set;
        }

        [JavaScript("WindowClosable", Category = "options", DefaultValue = true)]
        public bool? WindowClosable
        {
            get;
            set;
        }

        [JavaScript("WindowDraggable", Category = "options", DefaultValue = true)]
        public bool? WindowDraggable
        {
            get;
            set;
        }

        [JavaScript("WindowStatus", Category = "options", DefaultValue = "hidden")]
        public string WindowStatus
        {
            get;
            set;
        }

        [JavaScript("WindowAnimationSpeed", Category = "options", DefaultValue = 500)]
        public int WindowAnimationSpeed
        {
            get;
            set;
        }

        [JavaScript("WindowAnimation", Category = "options", DefaultValue = "easeOutElastic")]
        public string WindowAnimation
        {
            get;
            set;
        }

        [JavaScript("DeferLoad", Category = "options", DefaultValue = true)]
        public bool? DeferLoad
        {
            get
            {
                EnsureChildControls();
                return Updater.DeferLoad;
            }
            set
            {
                EnsureChildControls();
                Updater.DeferLoad = value;
            }
        }

        [JavaScript("CloseAfterSubmit", Category = "options", DefaultValue = true)]
        public bool? CloseAfterSubmit
        {
            get;
            set;
        }

        [JavaScript("WindowParentSelector", Category = "options")]
        public string WindowParentSelector
        {
            get;
            set;
        }


        [JavaScript("AutoSize", Category = "options", DefaultValue = false)]
        public bool? AutoSize
        {
            get;
            set;
        }

        public string ProgressControlID
        {
            get
            {
                EnsureChildControls();
                return this.Updater != null ? this.Updater.ProgressControlID : string.Empty;
            }
            set
            {
                EnsureChildControls();
                if (this.Updater != null)
                {
                    this.Updater.ProgressControlID = value;
                }
            }
        }

        public override ControlCollection Controls
        {
            get
            {
                EnsureChildControls();
                return DesignMode ? base.Controls : Updater.Controls;
            }
        }

        public AeroWindow()
        {
            Updater = new JUpdater();
            Updater.ID = "Updater";
        }

        protected override void CreateChildControls()
        {
            if (Updater == null)
            {
                Updater = new JUpdater();
                Updater.ID = "Updater";
            }
            Updater.Style.Add(HtmlTextWriterStyle.Width, "100%");
            Updater.Style.Add(HtmlTextWriterStyle.Height, "100%");

            base.Controls.Add(Updater);
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (AutoOpen.HasValue && !AutoOpen.Value)
            {
                this.Style.Add(HtmlTextWriterStyle.Display, "none");
            }
            base.RegisterJQueryResource("JQuery.WebKit.AeroWindow.js.jquery-AeroWindow.min.js");
            base.RegisterCSSResource("JQuery.WebKit.AeroWindow.css.AeroWindow.css");

            string script = string.Format("$('#{0}').AeroWindow({1});", this.ClientID, this.GetJSProperties("options"));
            this.Page.Resources.Add(new WebScriptResource(typeof(AeroWindow), this.ClientID, ResourceType.Javascript, script, LoadEvent.DocumentReady));

            if (AutoOpen.HasValue && AutoOpen.Value && (Page.IsPostBack || Page.IsRefresh))
            {
                script = string.Format("$('#{0}').AeroWindow('show');", this.ClientID);
                if (Page.IsPostBack || Page.IsRefresh)
                {
                    base.AddBodyScript(script);
                }
                else
                {
                    this.Page.Resources.Add(new WebScriptResource(typeof(AeroWindow), this.ClientID, ResourceType.Javascript, script, LoadEvent.DocumentReady));
                }
            }
            else if (CloseAfterSubmit.HasValue && CloseAfterSubmit.Value && Page.IsPostBack)
            {
                script = string.Format("$('#{0}').AeroWindow('hide');", this.ClientID);
                base.AddBodyScript(script);
            }

            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
        }
    }
}
